#include "cpdfpanel.hpp"
#include "cpdfreader.h"
const long CPdfPanel::BUTTON_PRE_PAGE = ::wxNewId();
const long CPdfPanel::BUTTON_NEXT_PAGE = ::wxNewId();
const long CPdfPanel::BUTTON_FIRST_PAGE = ::wxNewId();
const long CPdfPanel::BUTTON_END_PAGE = ::wxNewId();
const long CPdfPanel::SLIDER_SCALE = ::wxNewId();
const long CPdfPanel::TEXT_PAGE_NUM = ::wxNewId();
const long CPdfPanel::BUTTON_ROTATE_LEFT = ::wxNewId();
const long CPdfPanel::BUTTON_ROTATE_RIGHT = ::wxNewId();
const long CPdfPanel::MENU_ID_OPEN_FILE = ::wxNewId();
const long CPdfPanel::MENU_ID_CLOSE_FILE = ::wxNewId();
const long CPdfPanel::MENU_ID_NEXT_PAGE = ::wxNewId();
const long CPdfPanel::MENU_ID_PRE_PAGE = ::wxNewId();
const long CPdfPanel::MENU_ID_FIRST_PAGE = ::wxNewId();
const long CPdfPanel::MENU_ID_END_PAGE = ::wxNewId();
const long CPdfPanel::MENU_ID_ROTATE_LEFT = ::wxNewId();
const long CPdfPanel::MENU_ID_ROTATE_RIGHT = ::wxNewId();
const long CPdfPanel::BUTTON_WIDTH = 40;
const long CPdfPanel::CTRL_POS_OFFSET = 200;
const long CPdfPanel::ID_PDF_READER = ::wxNewId();

BEGIN_EVENT_TABLE( CPdfPanel , wxPanel )
  EVT_SIZE( CPdfPanel::_OnSize )
  EVT_BUTTON( BUTTON_PRE_PAGE , CPdfPanel :: OnPrePage )
  EVT_BUTTON( BUTTON_NEXT_PAGE , CPdfPanel :: OnNextPage )
  EVT_BUTTON( BUTTON_FIRST_PAGE , CPdfPanel :: OnFirstPage )
  EVT_BUTTON( BUTTON_END_PAGE , CPdfPanel :: OnEndPage)
  EVT_BUTTON( BUTTON_ROTATE_LEFT ,CPdfPanel::OnRotateLeft )
  EVT_BUTTON( BUTTON_ROTATE_RIGHT , CPdfPanel::OnRotateRight )
  EVT_COMMAND_SCROLL( SLIDER_SCALE , CPdfPanel::OnScale )
 // EVT_TEXT( TEXT_PAGE_NUM , CPdfPanel::OnTextPage )
  EVT_MENU( MENU_ID_OPEN_FILE , CPdfPanel::OnOpenFile )
  EVT_MENU( MENU_ID_CLOSE_FILE , CPdfPanel::OnCloseFile)
  EVT_MENU( MENU_ID_NEXT_PAGE , CPdfPanel::OnNextPage )
  EVT_MENU( MENU_ID_PRE_PAGE , CPdfPanel::OnPrePage )
  EVT_MENU( MENU_ID_FIRST_PAGE , CPdfPanel::OnFirstPage )
  EVT_MENU( MENU_ID_END_PAGE , CPdfPanel::OnEndPage )
  EVT_MENU( MENU_ID_ROTATE_RIGHT , CPdfPanel::OnRotateRight)
  EVT_MENU( MENU_ID_ROTATE_LEFT , CPdfPanel :: OnRotateLeft )
  EVT_RIGHT_UP(CPdfPanel::OnMouseRight )
END_EVENT_TABLE()

CPdfPanel::CPdfPanel()
{
    //ctor
}

CPdfPanel::~CPdfPanel()
{
  if( p_menu != NULL )
    delete p_menu;
}

void CPdfPanel :: SetPages( int page , int sum )
{
    TextPage->Clear();
    (*TextPage) << page + 1<< _("(") << sum << _(")");
}

CPdfPanel :: CPdfPanel( wxWindow *parent ,
			wxWindowID id ,
			const wxPoint& pos , const wxSize& _size ,
			long style , const wxString& name )
  :wxPanel( parent , id , pos , _size , style , name )
{
  wxSize size;
  size = GetClientSize();
  size.DecBy(0,25 );
  size_t tmp_x = size.GetWidth() / 2 - 200;

  ButtonFirst = new wxButton(this , BUTTON_FIRST_PAGE , _("<<") , wxPoint(tmp_x , size.GetHeight() + 2) ,wxSize(BUTTON_WIDTH , 20 ) , wxNO_BORDER);
  //ButtonFirst->Disable();
  tmp_x += BUTTON_WIDTH + 2;

  ButtonPre = new wxButton( this , BUTTON_PRE_PAGE , _("<") , wxPoint(tmp_x , size.GetHeight() + 2) , wxSize( BUTTON_WIDTH , 20 ) , wxNO_BORDER );
  //ButtonPre->Disable();
  tmp_x += BUTTON_WIDTH + 2;
  TextPage = new wxTextCtrl( this , TEXT_PAGE_NUM , wxEmptyString, wxPoint( tmp_x , size.GetHeight() + 2 ) , wxSize( 80 , 20 ) );
  wxTextAttr attr;
  TextPage->GetStyle( 0 , attr);
  attr.SetAlignment(wxTEXT_ALIGNMENT_CENTER );
  TextPage->SetDefaultStyle( attr );
  //TextPage->Disable();
  tmp_x += 82;

  ButtonNext = new wxButton( this , BUTTON_PRE_PAGE , _(">") , wxPoint( tmp_x , size.GetHeight() + 2) , wxSize( BUTTON_WIDTH , 20 ) , wxNO_BORDER );
  //ButtonNext->Disable();
  tmp_x = tmp_x + BUTTON_WIDTH + 2;
  ButtonEnd = new wxButton( this , BUTTON_END_PAGE , _(">>") , wxPoint( tmp_x , size.GetHeight() + 2) , wxSize( BUTTON_WIDTH , 20 ) , wxNO_BORDER );
  tmp_x = tmp_x + BUTTON_WIDTH + 2;

  SliderScale = new wxSlider( this , SLIDER_SCALE , 0 , -8 , 8 , wxPoint( tmp_x , size.GetHeight() + 2) , wxSize( 200 , 20 ) , wxNO_BORDER );
  //SliderScale->Disable();

  p_reader = new CPdfReader( this , ID_PDF_READER , wxPoint( 0 ,0 ) ,size );

  create_right_menu();
}

void CPdfPanel :: create_right_menu()
{
  p_menu = new wxMenu();

  p_menu->Append( MENU_ID_OPEN_FILE , _("打开PDF文件...") );
  p_menu->Append( MENU_ID_CLOSE_FILE , _("关闭文件"));
  p_menu->AppendSeparator();
  p_menu->Append( MENU_ID_NEXT_PAGE , _("下一页"));
  p_menu->Append( MENU_ID_PRE_PAGE , _("上一页"));
  p_menu->Append( MENU_ID_FIRST_PAGE , _("首页"));
  p_menu->Append( MENU_ID_END_PAGE , _("末页"));
  p_menu->Append( MENU_ID_ROTATE_LEFT , _("左旋转90°"));
  p_menu->Append( MENU_ID_ROTATE_RIGHT , _("右旋转90°"));
}

void CPdfPanel :: NextPage()
{
  p_reader->NextPage();
}


void CPdfPanel :: PrePage()
{
  p_reader->PrePage();
}

void CPdfPanel :: FirstPage()
{
  p_reader->GotoPage(0);
}

void CPdfPanel::EndPage()
{
  p_reader->GotoPage( p_reader->GetCount() - 1 );
}

void CPdfPanel :: GotoPage( int index )
{
  p_reader->GotoPage( index );
}

int CPdfPanel::GetCurrentPage()
{
  return p_reader->GetCurrentPage();
}

void CPdfPanel :: SetCurrentPage( int index )
{
  p_reader->GotoPage( index );
}

void CPdfPanel :: Scale( double s )
{
  p_reader->Scale( s );
}

void CPdfPanel :: OnNextPage( wxCommandEvent& evt )
{
  p_reader->NextPage();
}

void CPdfPanel :: OnPrePage( wxCommandEvent & evt )
{
  p_reader->PrePage();
}

void CPdfPanel :: OnScale( wxScrollEvent& evt )
{
  int var = SliderScale ->GetValue();
  double s = 1.0;

  if( var < 0 ){
    var = 8 + var;
    s = var / 8.0;
  }
  else if( var > 0 ){
    s = 1.0 * ( var + 0.5);
  }
  p_reader->Scale( s );

}

void CPdfPanel :: OnFirstPage( wxCommandEvent& evt )
{
  FirstPage();
}

void CPdfPanel :: OnEndPage( wxCommandEvent& evt )
{
  EndPage();
}

void CPdfPanel :: OnTextPage( wxCommandEvent& evt )
{
  long page;
  TextPage->GetValue().ToLong(&page , 10 );

  GotoPage( page );
}

void CPdfPanel::OnRotateLeft( wxCommandEvent& evt )
{
  poppler::rotation_enum ori;
  int ori_int;
  ori = p_reader->GetCurrentOrient();
  if( ori != poppler::rotate_0 ){
        ori_int = (int)ori;
        ori_int --;
        ori = (poppler::rotation_enum)ori_int;
  }
  else ori = poppler::rotate_270;

  p_reader->Rotate( ori );
}

void CPdfPanel::OnRotateRight( wxCommandEvent& evt )
{
  poppler::rotation_enum ori;
  int ori_int;
  ori = p_reader->GetCurrentOrient();
  if( ori != poppler::rotate_270 ){
        ori_int = (int)ori;
        ori_int --;
        ori = (poppler::rotation_enum)ori_int;
  }
  else ori = poppler::rotate_0;

  p_reader->Rotate( ori );
}

void CPdfPanel::OnMouseRight( wxMouseEvent& evt )
{
  PopupMenu(p_menu );
}

void CPdfPanel::OnOpenFile( wxCommandEvent& evt )
{
    wxFileDialog dlg( this , _("打开PDF文件") , _("") , _("") , _("pdf文件(*.pdf)|*.pdf") , wxFD_OPEN|wxFD_CHANGE_DIR);

    if( dlg.ShowModal() == wxID_OK ){
        wxString path;
        path = dlg.GetPath();
        if( path.IsEmpty() == false ){
            p_reader->Open( path );
            TextPage->Clear();
            *TextPage << _("1(") << p_reader->GetCount() << _(")");
        }
    }
}

void CPdfPanel::OnCloseFile( wxCommandEvent& evt )
{
    p_reader->CloseFile();
    p_reader->Refresh();
}

void CPdfPanel::_OnSize( wxSizeEvent& evt )
{
    wxSize size = GetClientSize();
    size.DecBy( 0,25 );
    size_t tmp_x = size.GetWidth() / 2 - 200;

    ButtonFirst->Move( tmp_x , size.GetHeight() + 2);
    tmp_x += BUTTON_WIDTH + 2;

    ButtonPre -> Move( tmp_x , size.GetHeight()+ 2 );
    tmp_x += BUTTON_WIDTH + 2;
    TextPage -> Move( tmp_x , size.GetHeight() + 2);
    tmp_x += 82;

    ButtonNext -> Move( tmp_x , size.GetHeight() + 2 );
    tmp_x = tmp_x + BUTTON_WIDTH + 2;
    ButtonEnd -> Move( tmp_x , size.GetHeight() + 2 );
    tmp_x = tmp_x + BUTTON_WIDTH + 2;

    SliderScale -> Move( tmp_x , size.GetHeight()+2);
    p_reader->SetSize( size );
    p_reader->Refresh();
}
